<template>
  <div class="banner-container">
    <div class="banner-top">
      <div class="banner-search"></div>
      <div class="banner-btn">
        <el-button type="primary" size="small" @click="isShowDialog = true">新增</el-button>
      </div>
    </div>
    <el-scrollbar style="height: calc(100vh - 160px)" class="scrollbar" id="scroll-bar">
      <el-table :data="list">
        <el-table-column label="id" prop="data_id"></el-table-column>
        <el-table-column label="关联菜单" prop="menu_name"></el-table-column>
        <el-table-column label="缩略图">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.url"
              style="width: 100px; height: 40px"
              :src="scope.row.url"
              :preview-src-list="[scope.row.url]"
              fit="cover"
            ></el-image>
            <span v-else>暂无图片</span>
          </template>
        </el-table-column>
        <el-table-column label="建议尺寸" width="160" prop="size">
          <template>
            <span style="text-align: center">1920 * 952</span>
          </template>
        </el-table-column>
        <el-table-column label="最后修改时间" width="160" prop="data_update_time"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-upload
              action=""
              class="avatar-uploader"
              :show-file-list="false"
              :before-upload="
                (file) => {
                  beforeAvatarUpload(file, scope.row.data_id);
                }
              "
            >
              <el-button size="mini">上传</el-button>
            </el-upload>
            <el-button type="primary" @click="updBanner(scope.row.data_id, scope.row.menu_id)" size="mini">
              修改
            </el-button>
            <el-button type="danger" @click="delBanner(scope.row.data_id)" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-scrollbar>

    <paging ref="page" :func="getList"></paging>
    <el-dialog @closed="onClose" title="提示" :visible.sync="isShowDialog">
      <el-form :model="params" :rules="rules" ref="dataParams" label-width="120px" class="demo-dataParams">
        <el-form-item label="菜单" prop="text">
          <el-select v-model="params.menu_id" placeholder="请选择菜单">
            <el-option
              v-for="item in menuList"
              :key="item.data_id"
              :label="item.menu_name"
              :value="item.data_id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowDialog = false">取 消</el-button>
        <el-button type="primary" @click="dialogConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import paging from '@/components/paging';
  import bannerManage from '@/api/banner';
  import menuManage from '@/api/menu';
  import { getImageUrl } from '@/utils/image';
  import { qiniuUploadFile } from '@/utils/QiniuUpload';
  export default {
    name: 'banner-view',
    components: {
      paging,
    },
    data() {
      return {
        list: [],
        menuList: [],
        isShowDialog: false,
        updId: '',
        params: { menu_id: '' },
        rules: {
          menu_id: [{ required: true, message: '请选择菜单', trigger: 'change' }],
        },
      };
    },
    async mounted() {
      this.refresh();
      this.getMenuList();
    },
    methods: {
      getImageUrl,
      updBanner(data_id, menu_id) {
        this.updId = data_id;
        this.isShowDialog = true;
        this.params = {
          menu_id: menu_id,
        };
      },
      async delBanner(data_id) {
        this.$alert('是否确定删除banner', '删除', {
          confirmButtonText: '确定',
          callback: async (action) => {
            if (action === 'confirm') {
              await bannerManage.delBanner(data_id);
              this.refresh();
            }
          },
        });
      },
      refresh() {
        this.$refs.page.refresh();
      },
      async getMenuList() {
        let res = await menuManage.getMenu();
        this.menuList = res.item_list;
      },
      getList(callback) {
        bannerManage.getBanner().then((res) => {
          let total = res.total_count;
          this.list = res.item_list;
          callback(this.list, total);
        });
      },
      async dialogConfirm() {
        this.$refs.dataParams.validate(async (valid) => {
          if (valid) {
            let res = null;
            if (this.updId) {
              res = await bannerManage.updBanner(this.updId, this.params);
            } else {
              res = await bannerManage.setBanner(this.params);
            }
            if (res) {
              this.isShowDialog = false;
              this.refresh();
            }
          }
        });
      },
      onClose() {
        this.updId = '';
        this.params = {
          text: '',
        };
      },
      async beforeAvatarUpload(file, id) {
        // 七牛云上传
        let res = await qiniuUploadFile(file, 'banner');
        if (res) {
          await bannerManage.updBanner(id, { url: res.key });
          this.refresh();
        }

        // const formData = new FormData();
        // formData.append('上传的key值', file);
        // await bannerManage.setBannerImage(id, formData);
        // this.refresh();
      },
      test() {
        let arr = [2, 3, 2, 4]; //每场选项所选个数
        let count = 2;
        let mult = 2; //倍数
        arr.forEach((item) => {
          if (item > 1) {
            count = count + (item - 1) * count;
          }
        });
        count = mult * count;
      },
    },
  };
</script>

<style lang="less" scoped>
  .banner-container {
    ::v-deep {
      .cell {
        display: flex;
        div {
          margin-right: 6px;
        }
      }
    }
    height: 100%;
    .avatar-uploader {
      .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .banner-top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 12px;
      .banner-search {
        display: flex;
      }
    }
    .banner-list {
      display: flex;
      flex-flow: row wrap;
      width: 100%;
      height: 100%;
      .banner-item {
        width: calc(50% - 6px);
        height: 100px;
        display: flex;
        margin-bottom: 6px;
        border: 1px solid #f3f3f3;
        .item-content {
          margin: 10px;
          p {
            line-height: 40px;
          }
        }
      }
    }
  }
</style>
